<template>
  <el-drawer v-model="isShow" size="40%" :title="title" direction="rtl" :before-close="handleClose">
    <el-form :model="formData" ref="form" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="community_code">
            <label style="width: 100%;"><span class="red">*</span>所属小区</label>
            <el-select style="width: 100%;" v-model="formData.community_code" @change="communityChange" class="m-2"
              placeholder="请选择">
              <el-option v-for="item in communityList" :key="item.id" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="building_code">
            <label style="width: 100%;"><span class="red">*</span>所属楼栋</label>
            <el-select style="width: 100%;" v-model="formData.building_code" @change="buildingChange" class="m-2"
              placeholder="请选择">
              <el-option v-for="item in buildingList" :key="item.id" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="house_code">
            <label style="width: 100%;"><span class="red">*</span>所属房产</label>
            <el-select style="width: 100%;" v-model="formData.house_code" @change="houseChange" class="m-2"
              placeholder="请选择">
              <el-option v-for="item in houseList" :key="item.id" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="house_code">
            <label style="width: 100%;"><span class="red">*</span>所属成员</label>
            <el-select style="width: 100%;" v-model="formData.member_id" class="m-2" placeholder="请选择">
              <el-option v-for="item in memberList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="name">
            <label style="width: 100%;"><span class="red">*</span>车辆名称</label>
            <el-input v-model="formData.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="license_plate">
            <label style="width: 100%;"><span class="red">*</span>车辆牌照</label>
            <el-input v-model="formData.license_plate" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="color">
            <label style="width: 100%;"><span class="red">*</span>车辆颜色</label>
            <el-input v-model="formData.color" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <label style="width: 100%;">备注</label>
            <el-input v-model="formData.remark" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item>
            <label style="width: 100%;">车辆照片</label>
            <!-- 图片组件 -->
            <uploadImg v-model:imgUrl="formData.photo" :limit="3" />
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
      </el-row>
      <div style="display: flex;justify-content: end;margin-top: 50px;">
        <el-form-item>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </el-form-item>
      </div>
    </el-form>

  </el-drawer>
</template>

<script setup>
import { usecommunityList } from '@/hooks/owners/member.js'
import { ref } from 'vue'
import { getBuildingListApi, getHouseListApi, getMemberListApi } from '@/api/owners/member.js'
import { addListApi, getListInfoApi, editListApi } from '@/api/owners/vehicle.js'
import uploadImg from '@/components/Table/uploadImg.vue'
import { ElMessage } from 'element-plus'

// =============================================================================
const isShow = ref(false)
const form = ref(null)
const title = computed(() => formData.value.id ? '修改车辆信息' : '添加车辆信息')
// 小区数据
const communityList = usecommunityList()
const defaultFormData = {
  building_code: "",
  community_code: "",
  house_code: "",
  color: "",
  license_plate: "",
  member_id: "",
  name: '',
  photo: "",
  remark: "",
  id: null
}
const rules = {
  community_code: [
    { required: true, message: '请选择', trigger: 'change' },
  ],
  building_code: [
    { required: true, message: '请选择', trigger: 'change' },
  ],
  house_code: [
    { required: true, message: '请选择', trigger: 'change' },
  ],
  color: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  license_plate: [
    { required: true, message: '请输入', trigger: 'change' },
    {
      pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/, message: "请填写正确的车牌号", trigger: "blur"
    },
  ],
  member_id: [
    { required: true, message: '请选择', trigger: 'change' },
  ],
  name: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
}
// 表单数据
let formData = ref({ ...defaultFormData })

const emit = defineEmits(['initData'])
// ==========================================================================
// 小区改变获取栋数
const buildingList = ref([])
const communityChange = async (val) => {
  formData.value.building_code = ''
  formData.value.house_code = ''
  // console.log(val);
  const { data } = await getBuildingListApi(val)
  // console.log(data);
  buildingList.value = data
}
// 栋数改变获取房产
const houseList = ref([])
const buildingChange = async (val) => {
  formData.value.house_code = ''
  const { data } = await getHouseListApi(val)
  // console.log(data);
  houseList.value = data
}

// 房产改变获取成员
const memberList = ref([])
const houseChange = async (val) => {
  const { data } = await getMemberListApi(val)
  console.log(data);
  memberList.value = data
}
// 添加===========修改
const onSubmit = async () => {
  try {
    await form.value.validate()
    formData.value.id ? await editListApi(formData.value) : await addListApi(formData.value)
    ElMessage.success(formData.value.id ? '修改成功' : '添加成功')
    isShow.value = false
    emit('initData')
  } catch (err) {
    console.log(err);
    ElMessage.error('必填项输入后才能提交喔')

  }
}

// 抽屉关闭前的回调
const handleClose = () => {
  form.value.resetFields()
  formData.value = { ...defaultFormData }
  isShow.value = false
}

// 修改回显
const edit = async (id) => {
  // console.log(id);
  const { data } = await getListInfoApi(id)
  // console.log(data);
  formData.value = data
  isShow.value = true
}
defineExpose({ isShow, edit })
</script>

<style lang="scss" scoped>
.red {
  color: red;
}
</style>